CREATE OR REPLACE TRIGGER cx_sysdefBI BEFORE INSERT ON cx_sysdef FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_sysdef.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/



CREATE OR REPLACE TRIGGER cx_syscfgBI BEFORE INSERT ON cx_syscfg FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_syscfg.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_syscacheBI BEFORE INSERT ON cx_syscache FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_syscache.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_entityBI BEFORE INSERT ON cx_entity FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_entity.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_fldBI BEFORE INSERT ON cx_fld FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_fld.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_fldvalueBI BEFORE INSERT ON cx_fldvalue FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_fldvalue.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_pluginBI BEFORE INSERT ON cx_plugin FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_plugin.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_funcBI BEFORE INSERT ON cx_func FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_func.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_organBI BEFORE INSERT ON cx_organ FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_organ.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/

CREATE or replace TRIGGER cx_organBU BEFORE UPDATE ON cx_organ FOR EACH ROW
DECLARE
v_code   VARCHAR(32) DEFAULT NULL;
changed number(4) DEFAULT 0;
BEGIN

  IF :OLD.code != :NEW.code THEN
    v_code := :NEW.code;
    changed := 1;
END IF;

IF changed = 1 THEN
UPDATE cx_userdep SET organname = v_code WHERE organid=:OLD.id;
END IF;
END;
/**/


CREATE OR REPLACE TRIGGER cx_organBD BEFORE DELETE ON cx_organ FOR EACH ROW
BEGIN
    DELETE FROM cx_userdep WHERE organid=:OLD.id;
    DELETE FROM cx_depadm  WHERE id0=:OLD.id;
END;
/**/


CREATE OR REPLACE TRIGGER cx_userBI BEFORE INSERT ON cx_user FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_user.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userAI AFTER INSERT ON cx_user FOR EACH ROW
  DECLARE
    v_defpsw      VARCHAR2(64);
    v_count       NUMBER(9);
  BEGIN
    SELECT count(1) INTO v_count FROM cx_syscfg WHERE no='DefPsw';
    IF v_count>0 THEN
      SELECT value INTO v_defpsw FROM cx_syscfg WHERE no='DefPsw';
    ELSE
      v_defpsw:='W9YDMCv0sIA7stOkxEfqCA==';   -- zbcx@15!
    END IF;

    INSERT INTO cx_sysuser(id,no,name,password) VALUES(:NEW.id,:NEW.no,:NEW.name,v_defpsw);
    INSERT INTO cx_userdep(userid,username,organid,organname) VALUES(:NEW.id,:NEW.name,:NEW.glid,(select namec from cx_organ where id=:NEW.glid));
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userAU AFTER UPDATE ON cx_user FOR EACH ROW
  DECLARE
    v_defpsw      VARCHAR2(64);
    v_count       NUMBER(9);
  BEGIN
    IF :OLD.state=0 AND :NEW.state=1 THEN
      UPDATE cx_sysuser SET password='state=1' WHERE id=:OLD.id;
    ELSIF :OLD.state=1 AND :NEW.state=0 THEN
      SELECT count(1) INTO v_count FROM cx_syscfg WHERE no='DefPsw';
      if v_count>0 then
        SELECT value INTO v_defpsw FROM cx_syscfg WHERE no='DefPsw';
      else
        v_defpsw:='W9YDMCv0sIA7stOkxEfqCA==';   -- zbcx@15!
      END IF;
      UPDATE cx_sysuser SET password=v_defpsw WHERE id=:OLD.id;
    END IF;

    IF :NEW.name != :OLD.name OR :NEW.no != :OLD.no THEN
      UPDATE cx_sysuser SET no=:NEW.no,name=:NEW.name WHERE id=:OLD.id;
      UPDATE cx_userdep SET username=:NEW.name WHERE userid=:OLD.id;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userBD BEFORE DELETE ON cx_user FOR EACH ROW
  BEGIN
    DELETE FROM cx_userdep   WHERE userid=:OLD.id;
    DELETE FROM cx_userrole  WHERE userid=:OLD.id;
    DELETE FROM cx_usergroup WHERE userid=:OLD.id;
    DELETE FROM cx_depadm    WHERE userid=:OLD.id;
    DELETE FROM cx_userhabit WHERE userid=:OLD.id;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userAD AFTER DELETE ON cx_user FOR EACH ROW
  BEGIN
    DELETE FROM cx_sysuser WHERE id=:OLD.id;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userdepBI BEFORE INSERT ON cx_userdep FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_userdep.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysroleBI BEFORE INSERT ON cx_sysrole FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_sysrole.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysroleBD BEFORE DELETE ON cx_sysrole FOR EACH ROW
  BEGIN
    DELETE FROM cx_userrole   WHERE roleid=:old.id;
    DELETE FROM cx_sysroleobj WHERE roleid=:old.id;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysroleAU AFTER UPDATE ON cx_sysrole FOR EACH ROW
  BEGIN
    IF :NEW.vry != :OLD.vry THEN
      UPDATE cx_userrole SET vry=:NEW.vry WHERE roleid=:NEW.id;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_depadmBI BEFORE INSERT ON cx_depadm FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_depadm.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_userroleBI BEFORE INSERT ON cx_userrole FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_userrole.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysroleobjBI BEFORE INSERT ON cx_sysroleobj FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_sysroleobj.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysgroupBI BEFORE INSERT ON cx_sysgroup FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_sysgroup.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_usergroupBI BEFORE INSERT ON cx_usergroup FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_usergroup.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_resrelBI BEFORE INSERT ON cx_resrel FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_resrel.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_maplayerBI BEFORE INSERT ON cx_maplayer FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_maplayer.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_mapserviceBI BEFORE INSERT ON cx_mapservice FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_mapservice.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_viewportBI BEFORE INSERT ON cx_viewport FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_viewport.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_regionBI BEFORE INSERT ON cx_region FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_region.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_layerBI BEFORE INSERT ON cx_layer FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_layer.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_maparaBI BEFORE INSERT ON cx_mapara FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_mapara.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_loginlogBI BEFORE INSERT ON cx_loginlog FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_loginlog.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_operlogBI BEFORE INSERT ON cx_operlog FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_operlog.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_sysdsBI BEFORE INSERT ON cx_sysds FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_sysds.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_mediaBI before INSERT ON cx_media FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_media.NEXTVAL INTO :new.id FROM DUAL;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_urlmapBI before INSERT ON cx_urlmap FOR EACH ROW
  BEGIN
    IF :NEW.id IS NULL THEN
      SELECT seqcx_urlmap.NEXTVAL INTO :new.id FROM DUAL;
    END IF;
  END;
/**/




CREATE OR REPLACE TRIGGER cx_coderuleBI BEFORE INSERT ON cx_coderule FOR EACH ROW
BEGIN
    IF :NEW.ID IS NULL THEN
        SELECT seqcx_coderule.NEXTVAL INTO :NEW.ID FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_sqlexpBI BEFORE INSERT ON cx_sqlexp FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_sqlexp.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_sqlproBI BEFORE INSERT ON cx_sqlpro FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_sqlpro.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_authexpBI BEFORE INSERT ON cx_authexp FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_authexp.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_authettBI BEFORE INSERT ON cx_authett FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_authett.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_ruletabBI BEFORE INSERT ON cx_ruletab FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_ruletab.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_cmdtoolBI BEFORE INSERT ON cx_cmdtool FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_cmdtool.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_resdirBI BEFORE INSERT ON cx_resdir FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_resdir.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_userhabitBI BEFORE INSERT ON cx_userhabit FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_userhabit.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_smsconfigBI BEFORE INSERT ON cx_smsconfig FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_smsconfig.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/




CREATE OR REPLACE TRIGGER cx_updateBI BEFORE INSERT ON cx_update FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_update.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/



CREATE OR REPLACE TRIGGER cx_update_hisBI BEFORE INSERT ON cx_update_his FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_update_his.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_validatorBI BEFORE INSERT ON cx_validator FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_validator.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_datafmtBI BEFORE INSERT ON cx_datafmt FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_datafmt.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_datavslBI BEFORE INSERT ON cx_datavsl FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_datavsl.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/


CREATE OR REPLACE TRIGGER cx_tabcfgBI BEFORE INSERT ON cx_tabcfg FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_tabcfg.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_tabcfgBI BEFORE INSERT ON cx_tabcfg FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_tabcfg.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_resvalBI BEFORE INSERT ON cx_resval FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_resval.NEXTVAL INTO :NEW.id FROM DUAL;
    END IF;
END;
/**/

CREATE OR REPLACE TRIGGER cx_procgrpBI BEFORE INSERT ON cx_procgrp FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_procgrp.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/


CREATE OR REPLACE TRIGGER cx_procsBI BEFORE INSERT ON cx_procs FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_procs.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/


CREATE OR REPLACE TRIGGER cx_delegateBI BEFORE INSERT ON cx_delegate FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_delegate.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/



CREATE OR REPLACE TRIGGER cx_procassiBI BEFORE INSERT ON cx_procassi FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_procassi.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/



CREATE OR REPLACE TRIGGER cx_proccompBI BEFORE INSERT ON cx_proccomp FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_proccomp.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/



CREATE OR REPLACE TRIGGER cx_seqbillBI BEFORE INSERT ON cx_seqbill FOR EACH ROW
BEGIN
    IF :NEW.id IS NULL THEN
        SELECT seqcx_seqbill.NEXTVAL INTO :NEW.id FROM dual;
    END IF;
END;
/**/